package com.rgs.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.rgs.ssm.domain.Product;
import com.rgs.ssm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * @create 2021-02-15 17:22
 **/
@Controller
@RequestMapping("/product")
public class ProductController {
    
    @Autowired
    private IProductService productService;


    /**
     * 查询全部的商品信息
     * @return
     */
    @RequestMapping("/findAll.do")
    @RolesAllowed("ADMIN")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page, @RequestParam(name = "size",required =true,defaultValue = "5")Integer size){
        ModelAndView mv = new ModelAndView();
        List<Product> products = productService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(products);
        mv.addObject("productList",pageInfo);
        mv.setViewName("product-list");
        return mv;
    }

    /**
     * 添加产品
     * @param product
     */
    @RequestMapping("/save.do")
    public String saveProduct(Product product){
        productService.saveProduct(product);
        System.out.println("添加商品完毕！");
        return "redirect:/product/findAll.do";
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/deleteById.do")
    public String  deleteById(@RequestParam(name = "id",required = true)Integer id){
        productService.delete(id);
        return "redirect:/product/findAll.do";
    }

    /**
     * 去修改页面
     * @return
     */
    @RequestMapping("/toUpdateById.do")
    public ModelAndView toUpdateById(@RequestParam(name = "id",required = true)Integer id){
        ModelAndView mv = new ModelAndView();
        Product product = productService.findById(id);
        mv.addObject("product",product);
        mv.setViewName("product-update");
        return mv;
    }

    /**
     * 处理修改请求
     * @return
     */
    @RequestMapping("/updateById.do")
    public String updateById(Product product){
        productService.update(product);
        return "redirect:/product/findAll.do";
    }

    /**
     * 根据id，查看指定用户详情（包括用户的角色信息，以及角色信息中的权限信息）
     * @param userId
     * @return
     */
    @RequestMapping("/showDetails.do")
    public ModelAndView showDetails(@RequestParam(name = "id",required = true)Integer userId){
        ModelAndView mv = new ModelAndView();
        Product product = productService.findById(userId);
        mv.addObject("product",product);
        mv.setViewName("product-show");
        return mv;
    }


}
